package com.sunchangming.uniqname;

import java.io.IOException;
import java.nio.file.Files;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.io.xml.StaxDriver;

public class ServiceConfig implements ServiceConfigMXBean {
	/** server only */
	@XStreamAsAttribute
	private String jdbcUrl;
	/** server only */
	@XStreamAsAttribute
	private String jdbcusername;
	/** server only */
	@XStreamAsAttribute
	private String jdbcpassword;
	/** server only */
	@XStreamAsAttribute
	private String jdbcencoding;
	/** client only */
	@XStreamAsAttribute
	private int rpcClientWaitTimeout;
	/** client only */
	@XStreamAsAttribute
	private int rpcClientWriteTimeout;
	/** client only */
	@XStreamAsAttribute
	private int connectTimeout;
	/** client only */
	@XStreamAsAttribute
	private int connectRetryTimes;

	public int getRpcClientWaitTimeout() {
		return rpcClientWaitTimeout;
	}

	public int getRpcClientWriteTimeout() {
		return rpcClientWriteTimeout;
	}

	public int getConnectTimeout() {
		return connectTimeout;
	}

	public int getConnectRetryTimes() {
		return connectRetryTimes;
	}

	private static volatile ServiceConfig instance;

	static void init(java.nio.file.Path configFile) throws IOException {
		XStream xstream = new XStream(new StaxDriver());
		xstream.alias("config", ServiceConfig.class);
		xstream.autodetectAnnotations(true);
		instance = (ServiceConfig) xstream.fromXML(Files.newInputStream(configFile));
	}

	public static ServiceConfig getInstance() {
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunchangming.uniqname.ServiceConfigMXBean#getJdbcUrl()
	 */
	@Override
	public String getJdbcUrl() {
		return jdbcUrl;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunchangming.uniqname.ServiceConfigMXBean#getJdbcusername()
	 */
	@Override
	public String getJdbcusername() {
		return jdbcusername;
	}

	public String getJdbcpassword() {
		return jdbcpassword;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunchangming.uniqname.ServiceConfigMXBean#getJdbcencoding()
	 */
	@Override
	public String getJdbcencoding() {
		return jdbcencoding;
	}

}
